import 'package:flutter/material.dart';
import 'package:flutter_douban/config/Http_service.dart';
import 'package:flutter_douban/config/app_colors.dart';
import 'package:flutter_douban/loading/custom_header.dart';
import 'package:flutter_douban/main_page/class/view/class_category_view.dart';
import 'package:flutter_douban/main_page/class/view/class_cell_view.dart';
import 'package:flutter_douban/main_page/store/view/store_nav_view.dart';
import 'package:flutter_douban/main_page/store/view/stroe_category_view.dart';
import 'package:flutter_douban/main_page/store/view/stroe_product_cell.dart';
import 'package:flutter_douban/main_page/store/view/stroe_seckill_view.dart';
import 'package:flutter_douban/model/store_category_model.dart';
import 'package:flutter_douban/tool_views/back_nav_btn.dart';
import 'package:flutter_douban/tool_views/stance_page.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

class StorePage extends StatefulWidget {
  const StorePage({Key? key}) : super(key: key);

  @override
  _StorePageState createState() => _StorePageState();
}

class _StorePageState extends State<StorePage> {
  /// 控制器
  EasyRefreshController? _easyRefreshController;
  List<StoreCategoryModel> _topCategoryList = StoreCategoryModelList([]).list;
  List<StoreCategoryModel> _miaoList = StoreCategoryModelList([]).list;
  List<StoreCategoryModel> _storeList = StoreCategoryModelList([]).list;

  Future _getTopData() async {
    try {
      Map<String, dynamic> result = await HttpService.getMockDataWithPath(
          path: 'mock/store_top_data.json');
      StoreCategoryModelList topcateModelList =
          StoreCategoryModelList.fromJson(result['result']['cs']);
      StoreCategoryModelList miaoModelList =
          StoreCategoryModelList.fromJson(result['result']['rps'][0]['ps']);
      setState(() {
        _topCategoryList = topcateModelList.list;
        _miaoList = miaoModelList.list;
      });

      /// 结束加载状态

    } catch (e) {
      print(e);
    } finally {}
  }

  Future _getListData() async {
    try {
      /// 全部数据
      Map<String, dynamic> result = await HttpService.getMockDataWithPath(
          path: 'mock/store_list_data.json');
      List mapList = result['result']['rps'];
      List<dynamic> dataList = [];
      for (int i = 0; i < mapList.length; i++) {
        Map map = mapList[i];
        dataList.add(map['rp']);
      }
      StoreCategoryModelList storeModelList =
          StoreCategoryModelList.fromJson(dataList);
      setState(() {
        _storeList = storeModelList.list;
      });

      /// 结束加载状态
    } catch (e) {
      print(e);
    } finally {}
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    /// 头部数据
    _getTopData();

    /// 列表数据
    _getListData();
  }

  @override
  Widget build(BuildContext context) {
    Widget widget;
    if (_topCategoryList.length > 0 &&
        _miaoList.length > 0 &&
        _storeList.length > 0) {
      List<Widget> widgetList = [];

      /// 分类视图
      widgetList.add(StoreCategoryView(list: _topCategoryList,));

      /// 秒杀
      widgetList.add(StroeSeckillView(list: _miaoList,));

      widgetList.add(Container(
        padding: EdgeInsets.only(left: 20, bottom: 6,top: 10),
        alignment: Alignment.centerLeft,
        child: Text("全部商品",
            style: TextStyle(
                fontSize: 20,
                color: Colors.black,
                fontWeight: FontWeight.bold)),
      ));

      widgetList.add(Container(
        color: AppColors.page,
        padding: EdgeInsets.only(left: 14, right: 14, top: 10),
        child: GridView.builder(
            itemCount: _storeList.length,
            primary: false,
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                //横轴元素个数
                crossAxisCount: 2,
                //纵轴间距
                mainAxisSpacing: 1.0,
                //横轴间距
                crossAxisSpacing: 15.0,
                //子组件宽高长度比例
                childAspectRatio: 0.57),
            itemBuilder: (BuildContext context, int index) {
              return StroeProductCell(model: _storeList[index],);
            }),
      ));

      widget = EasyRefresh(
          firstRefresh: false,
          header: CustomHeaderView(),
          controller: _easyRefreshController,
          enableControlFinishRefresh: false,
          enableControlFinishLoad: true,
          onRefresh: _getListData,
          child: Column(
            children: widgetList,
          ));
    } else {
      widget = StancePage();
    }

    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: StoreNavView(),
      ),
      body: widget,
    );
  }
}
